import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/routers/fluro_navigator.dart';
import 'package:flutter_deer/setting/iview/version_iview.dart';
import 'package:flutter_deer/setting/models/version_entity.dart';
import 'package:flutter_deer/setting/presenter/version_presenter.dart';
import 'package:flutter_deer/setting/provider/locale_provider.dart';
import 'package:flutter_deer/setting/provider/theme_provider.dart';
import 'package:flutter_deer/setting/provider/version_provider.dart';
import 'package:flutter_deer/setting/widgets/exit_dialog.dart';
import 'package:flutter_deer/setting/widgets/update_dialog.dart';
import 'package:flutter_deer/util/device_utils.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/click_item.dart';
import 'package:flutter_deer/widgets/my_app_bar.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';
import 'package:sp_util/sp_util.dart';
import 'package:package_info/package_info.dart';

import '../setting_router.dart';

class SettingPage extends StatefulWidget {

  const SettingPage({super.key});

  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage>  with BasePageMixin<SettingPage, VersionPresenter> implements VersionIMvpView{

  @override
  VersionPresenter createPresenter() => VersionPresenter();
  VersionProvider versionProvider = VersionProvider();

  @override
  void setVersion(VersionEntity? entity){
    versionProvider.setVersion(entity);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
          centerTitle: '${DeerLocalizations.of(context)!.setUp.nullSafe}',
          isCustomBack: true,
          onBackPressed: () {
            NavigatorUtils.goBackWithParams(context, 'refreshData');
          }
      ),
      body: Consumer2<ThemeProvider, LocaleProvider>(
        builder: (_, ThemeProvider provider, LocaleProvider localeProvider, __) {
          return Column(
            children: <Widget>[
              Gaps.vGap5,
              ClickItem(
                title: '${DeerLocalizations.of(context)!.accountManagement.nullSafe}',
                onTap: () => NavigatorUtils.push(context, SettingRouter.accountManagerPage),
              ),
              if (Device.isMobile) ClickItem(
                title: '${DeerLocalizations.of(context)!.clearCache.nullSafe}',
                content: '23.5MB',
                onTap: () {},
              ),
              ClickItem(
                title: '${DeerLocalizations.of(context)!.nightMode.nullSafe}',
                content: _getCurrentTheme(),
                onTap: () => NavigatorUtils.push(context, SettingRouter.themePage),
              ),
              ClickItem(
                title: '${DeerLocalizations.of(context)!.language.nullSafe}',
                content: _getCurrentLocale(),
                onTap: () => NavigatorUtils.push(context, SettingRouter.localePage),
              ),
              if (Device.isMobile)
                ChangeNotifierProvider<VersionProvider>(
                  create: (_) => versionProvider,
                  child: Consumer<VersionProvider>(
                    builder: (_, versionProvider, child) {
                      if(versionProvider.stateType==StateType.loading){
                        return ClickItem(title: '${DeerLocalizations.of(context)!.versionInfoObtaining.nullSafe}',);
                      }
                      return  ClickItem(
                        displayCircle: versionProvider.currentVersion.nullSafe != versionProvider.version!.versionNum,
                        title: versionProvider.currentVersion.nullSafe==versionProvider.version!.versionNum?'${DeerLocalizations.of(context)!.latestVersion.nullSafe}(${versionProvider.currentVersion.nullSafe})':'${DeerLocalizations.of(context)!.clickUpdate.nullSafe}(${versionProvider.currentVersion.nullSafe})',
                        onTap: _showUpdateDialog,
                      );
                    }
                  )
                ),

              // ClickItem(
              //   title: '关于我们',
              //   onTap: () => NavigatorUtils.push(context, SettingRouter.aboutPage),
              // ),
              ClickItem(
                title: '${DeerLocalizations.of(context)!.exitCurrentAccount.nullSafe}',
                onTap: _showExitDialog,
              ),
              // if (Device.isMobile) ClickItem(
              //   title: 'Deer Web版',
              //   onTap: () => NavigatorUtils.goWebViewPage(context, 'Flutter Deer', 'https://simplezhli.github.io/flutter_deer/'),
              // ),
              // ClickItem(
              //   title: '其他Demo',
              //   onTap: () => AppNavigator.push(context, const DemoPage()),
              // ),
            ],
          );
        },
      ),
    );
  }

  String _getCurrentTheme() {
    final String? theme = SpUtil.getString(Constant.theme);
    String themeMode;
    switch(theme) {
      case 'Dark':
        themeMode = '${DeerLocalizations.of(context)!.open.nullSafe}';
        break;
      case 'Light':
        themeMode = '${DeerLocalizations.of(context)!.close.nullSafe}';
        break;
      default:
        themeMode = '${DeerLocalizations.of(context)!.languageLocal.nullSafe}';
        break;
    }
    return themeMode;
  }

  String _getCurrentLocale() {
    final String? locale = SpUtil.getString(Constant.locale);
    String localeMode;
    switch(locale) {
      case 'zh':
        localeMode = '${DeerLocalizations.of(context)!.languageZh.nullSafe}';
        break;
      case 'en':
        localeMode = '${DeerLocalizations.of(context)!.languageEn.nullSafe}';
        break;
      case 'vi':
        localeMode = '${DeerLocalizations.of(context)!.languageVi.nullSafe}';
        break;
      default:
        localeMode = '${DeerLocalizations.of(context)!.languageLocal.nullSafe}';
        break;
    }
    return localeMode;
  }

  void _showExitDialog() {
    showDialog<void>(
      context: context,
      builder: (_) => const ExitDialog()
    );
  }

  void _showUpdateDialog() {
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      if(packageInfo.version==versionProvider.version!.versionNum.nullSafe){
        showToast('${DeerLocalizations.of(context)!.noUpdateRequired.nullSafe}');
      }else{
        showDialog<void>(
          context: context,
          barrierDismissible: false,
          builder: (_) => UpdateDialog(versionEntity: versionProvider.version,)
        );
      }
    });

  }
}
